package com.pda.common_easy.pages.main;

import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.pda.common_easy.PDApp;
import com.pda.common_easy.R;
import com.pda.common_easy.beanout.LoginBeanOut;
import com.pda.common_easy.database.DataBaseControl;
import com.pda.common_easy.message.MessageWarp;
import com.pda.common_easy.pages.inbill.InBillActivity;
import com.pda.common_easy.pages.outbill.OutBillActivity;
import com.pda.common_easy.pages.pro.ProBillActivity;
import com.pda.common_easy.pages.returnscanf.ReturnScanfActivity;
import com.public_module.basemvp.activity.BaseActivity_noMVP;
import com.public_module.config.ApiYJ;
import com.public_module.config.SetConfig;
import com.public_module.dialogs.ChooseDialog;
import com.public_module.tools.CheckVersion;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import com.shelwee.update.UpdateHelper;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends BaseActivity_noMVP {
    @BindView(R.id.tvr)
    TextView tvr;
    @BindView(R.id.gridview1)
    GridView gridview1;
    private String TAG = "MainActivity";

    @BindView(R.id.llmain)
    LinearLayout llmain;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;

    ChooseDialog chooseDialog1;
    List<LoginBeanOut.DataDTO.ShowInfoDTO> list = new ArrayList<>();
    Gson gson;
    OkHttpClient client;
    ApiYJ apisync;
    public static MainActivity mainActivity;

    protected void initdata() {
        tvTitle.setText("主页");
        list = (List<LoginBeanOut.DataDTO.ShowInfoDTO>) getIntent().getSerializableExtra(PDApp.showListKey);
        MainAdapter mainAdapter = new MainAdapter(this, list);
        gridview1.setAdapter(mainAdapter);


        EventBus.getDefault().register(this);
        chooseDialog = new ChooseDialog(MainActivity.this, "是否退出程序", R.style.CommentDialog, new ChooseDialog.PriorityListener() {
            @Override
            public void setActivityText() {
                chooseDialog.dismiss();
                MainActivity.this.finish();
            }
        });
        WindowManager.LayoutParams lp = chooseDialog.getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;

        chooseDialog1 = new ChooseDialog(MainActivity.this, "是否清除缓存", R.style.CommentDialog, new ChooseDialog.PriorityListener() {
            @Override
            public void setActivityText() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        chooseDialog1.dismiss();
                        DataBaseControl.clearData();
                        Glide.get(MainActivity.this).clearMemory();
//                        Glide.get(MainActivity.this).clearDiskCache();
                        showMsg("已成功清除缓存");
                    }
                });

            }
        });
        WindowManager.LayoutParams lp1 = chooseDialog1.getWindow().getAttributes();
        lp1.gravity = Gravity.CENTER;

        gson = new Gson();
        client = new OkHttpClient.Builder()
                .connectTimeout(SetConfig.getConnectTimeout(), TimeUnit.SECONDS)
                .readTimeout(SetConfig.getReadTimeout(), TimeUnit.SECONDS)
                .writeTimeout(SetConfig.getWriteTimeout(), TimeUnit.SECONDS)
                .followRedirects(false)
                .build();
        apisync = new Retrofit.Builder()
                .baseUrl("http://" + SharedPreferencesManger.getHostName() + "/")
                .client(client)
                .build().create(ApiYJ.class);

        UpdateHelper.setPackageName("com.pda.common_easy");
        UpdateHelper updateHelper = new UpdateHelper.Builder(this)
                .checkUrl("http://" + SharedPreferencesManger.getHostName() + "/pda/APPSoftUpdate.ashx?cid=" + SharedPreferencesManger.getCid())
                .isAutoInstall(true)
                .build();
        updateHelper.check();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mainActivity = this;
        initdata();
//        if(SharedPreferencesManger.getUpass().equals("000000")){
//            intentToActivityWithoutParameter(this, ModfiyPassActivity.class);
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            chooseDialog.show();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @OnClick({R.id.iv_title_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBaseMessageEvent(MessageWarp messageWarp) {
        switch (messageWarp.getCode()) {
            case 79111:
                intentToActivityWithoutParameter(this, InBillActivity.class);
                break;
            case 7912:
                intentToActivityWithoutParameter(this, OutBillActivity.class);
                break;
            case 7918:
                intentToActivityWithoutParameter(this, ProBillActivity.class);
                break;
            default:
                break;
        }
    }
}
